using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;

namespace produccion.clases
{

    class CLSPREMTPP : ObservableObject
    {

MDOgeneral mod = new MDOgeneral();

#region Metodos basicos tabla
   
    public int actualizar()
    {
        try
        {
            string query = "update PREMTPP set VALOR_PMP="+VALOR_PMP+" where CODLIS_PMP='"+CODLIS_PMP+"' and CODMAT_PMP='"+CODMAT_PMP+"' and CODCOL_PMP='"+CODCOL_PMP+"'";
            return mod.comando(query);
        }
        catch (Exception e) { throw e; }
    }
   
    public int insertar()
    {
        try
        {
            string query = "insert into PREMTPP (CODLIS_PMP,CODMAT_PMP,CODCOL_PMP,VALOR_PMP) values ('" + CODLIS_PMP + "','" + CODMAT_PMP + "','" + CODCOL_PMP + "'," + VALOR_PMP + ")";
            return mod.comando(query);
        }
        catch (Exception e) { throw e; }
    }

    public Boolean existe() 
    {
        try
        {
            string query = "select count(*) from PREMTPP where CODLIS_PMP='"+CODLIS_PMP+"' and CODMAT_PMP='"+CODMAT_PMP+"' and CODCOL_PMP='"+CODCOL_PMP+"'";
            DataTable val = mod.consulta(query);

            if (val.Rows[0][0].ToString().Equals("0"))
            {
                  return  false;
            }
            else
            {
                 return  true;
            }
            
        }
        catch (Exception e) { throw e; }
    }

    public void conCosto()
    {
        try
        {
            string query = "select COALESCE(VALOR_PMP,0) as VALOR from PREMTPP where CODLIS_PMP='99' and CODMAT_PMP='" + CODMAT_PMP + "' and  CODCOL_PMP='" + CODCOL_PMP + "'";
            DataTable val = mod.consulta(query);
            if (val.Rows.Count > 0)
            {
                this.VALOR_PMP = Convert.ToDouble(val.Rows[0]["VALOR"]);
                EXISTE = true;
                CAMBIO = false;
            }
            else
            {
                EXISTE = false;
            }
        }
        catch (Exception e) { throw e; }
    }


    public DataTable conLisPre()
    {
        try
        {
            string query = "select CODMAT_MTP,CODCOL_MTP,(select NOMCOL_COL from maecolp where CODCOL_COL=CODCOL_MTP) as NOMCOL,VALOR_PMP,CODLIS_PRE,NOMLIS_PRE,TIPLIS_PRE from LISPREP full outer join (select CODMAT_MTP,CODCOL_MTP,CODLIS_PMP,VALOR_PMP from PREMTPP right outer join MAEMTP on CODMAT_PMP=CODMAT_MTP and CODCOL_PMP=CODCOL_MTP where CODMAT_MTP='" + CODMAT_PMP + "') on CODLIS_PRE =CODLIS_PMP where (APLICAA_PRE='M' OR APLICAA_PRE is null) Order by TIPLIS_PRE";
            DataTable val = mod.consulta(query);
            if (val.Rows.Count > 0)
            {
                EXISTE = true;
                CAMBIO = false;
                return val;
            }
            else
            {
                EXISTE = false;
            }
            return val;
        }
        catch (Exception ex) { throw ex; }
    }

    public int guardar(List<clases.CLSPREMTPP> LisPre) {

        try
        {
            MDOgeneral.transaccion=MDOgeneral.conexion.BeginTransaction();

            for (int i = 0; i <= LisPre.Count - 1; i++) {
               
                if (LisPre[i].existe())
                {
                    LisPre[i].actualizar();
                }
                else {
                    LisPre[i].insertar();                   
                }
            }
            MDOgeneral.transaccion.Commit();
        }
        catch (Exception ex) {
            
            MDOgeneral.transaccion.Rollback();
            throw (ex);
        }
        return 1;
    }

#endregion

#region  Propiedades tabla PREMTPP

    private string _CODLIS_PMP;
    public string CODLIS_PMP
    {
        get { return _CODLIS_PMP; }
        set
        {   _CODLIS_PMP = value;
            RaisePropertyChangedEvent("CODLIS_PMP");
        }
    }

    private string _CODMAT_PMP;
    public string CODMAT_PMP
    {
        get { return _CODMAT_PMP; }
        set {
            if (_CODMAT_PMP != value) { CAMBIO = true; }
            _CODMAT_PMP = value;
            RaisePropertyChangedEvent("CODMAT_PMP");
        }
    }

    private string _CODCOL_PMP;
    public string CODCOL_PMP
    {
        get { return _CODCOL_PMP; }
        set
        {
            if (_CODCOL_PMP != value) { CAMBIO = true; }
            _CODCOL_PMP = value;
            RaisePropertyChangedEvent("CODCOL_PMP");
        }
    }

    private double _VALOR_PMP;
    public double VALOR_PMP
    {
        get { return _VALOR_PMP; }
        set
        {
            if (_VALOR_PMP != value) { CAMBIO = true; }
            _VALOR_PMP = value;
            RaisePropertyChangedEvent("VALOR_PMP");
        }
    }

    private Boolean _CAMBIO=false;
    public Boolean CAMBIO
    {
        get { return _CAMBIO; }
        set
        {
            _CAMBIO = value;
            RaisePropertyChangedEvent("CAMBIO");
        }
    }

    private Boolean _EXISTE=false;
    public Boolean EXISTE
    {
        get { return _EXISTE; }
        set
        {
            _EXISTE = value;
            RaisePropertyChangedEvent("EXISTE");
        }
    }

#endregion

      }
}
